package com.hulk.callback;

import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

public class MqConfirmCallback implements RabbitTemplate.ConfirmCallback {

	/**
	 * 通过回调correlationData字段获取发送的消息Exchange名称
	 * @param correlationData
	 * @return
	 */
	private Map<String,String> getMQMsg(String correlationData) {
		Map<String,String> mqMsg = new HashMap<String,String>();
		if(correlationData == null || "".equals(correlationData)){
			return mqMsg;
		}
		int index = correlationData.indexOf(":");
		if(index == -1){
			return mqMsg;
		}
		mqMsg.put("msgExchange", correlationData.substring(0, index));
		mqMsg.put("msgBody", correlationData.substring(index+1,correlationData.length()));
		return mqMsg;
	 }


	/* (non-Javadoc)
	 * @see org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback#confirm(org.springframework.amqp.rabbit.support.CorrelationData, boolean, java.lang.String)
	 */
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String arg2) {
//		String msg = correlationData.getId();
//		Map<String,String> msgMap = this.getMQMsg(msg);
		if (ack) {
			System.out.println("写入MQ消息成功：");
		} else {
			System.out.println("写入MQ消息失败：");
		}
	}
}
